<template>
	<!-- 我的余额 -->
	<view class="balance-main">
		<view class="balance-option" v-if="isOpen">
			<view class="balance-option-item" @click="goToPath('/pages/bankCard/bankCard')">
				<uni-icons type="wallet-filled" size="34" color="#fff"></uni-icons>
				<text style="margin-top: 15rpx">银行卡</text>
				<text style="margin-top: 10rpx">{{ cardList.length }}张</text>
			</view>
			<view class="balance-option-item" @click="goToPath('/pages/balanceDetail/balanceDetail')">
				<uni-icons type="wallet-filled" size="34" color="#fff"></uni-icons>
				<text style="margin-top: 15rpx">账户余额</text>
				<text style="margin-top: 10rpx; color: #f3f3f3">￥{{ balanceValue }}</text>
			</view>
		</view>

		<view class="balance" v-else>
			<u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" text="尚未开通钱包"></u-empty>
		</view>

		<view class="accountSetting-Trading">
			<view class="settingTitle">交易设置</view>
			<view class="settingList">
				<view class="settingItem" @click="getAuthentication()">
					<view class="settingItem-left">
						<uni-icons type="auth-filled" size="24" class="icons" color="#2979ff"></uni-icons>
						<text>实名认证</text>
					</view>
					<view class="settingItem-rigth">
						<uni-tag v-if="!userInfo.idcard" text="未实名" size="mini"></uni-tag>
						<uni-tag text="已实名" v-else type="primary" size="mini"></uni-tag>
						<uni-icons type="forward" size="20" class="icons" color="#444"></uni-icons>
					</view>
				</view>
				<view class="settingItem" @click="goToPath('/pages/bankCard/bankCard')" style="border: none">
					<view class="settingItem-left">
						<uni-icons type="wallet" size="24" class="icons" color="#ffb404"></uni-icons>
						<text>银行卡列表</text>
					</view>
					<view class="settingItem-rigth">
						<text>{{ cardList.length }}张</text>
						<uni-icons type="forward" size="20" class="icons" color="#444"></uni-icons>
					</view>
				</view>
			</view>
		</view>

		<view class="balance-btn" :style="bottomStyle" v-if="!isOpen">
			<button type="default" style="background-color: #07c160; color: #fff" @click="goToPath('/pages/openWallet/openWallet')">开通钱包</button>
		</view>
	</view>
</template>

<script>
import { userNumCount } from '../../api/home/index.js';
import { formatAmount } from '../../utils/tools.js';
import { paybankCardList } from '../../api/payment/index.js';
export default {
	data() {
		return {
			isOpen: false,
			balanceValue: '',
			cardList: [],
			userInfo: {}
		};
	},
	computed: {
		bottomStyle() {
			const systemInfo = uni.getSystemInfoSync();
			const { safeAreaInsets } = systemInfo;
			const bottomSafeAreaHeight = safeAreaInsets.bottom || 0;
			return `padding-bottom: ${bottomSafeAreaHeight ? bottomSafeAreaHeight + 'px' : '20rpx'}`;
		}
	},
	async onShow() {
		const userData = (await uni.getStorageSync('userInfo')) || '';
		this.userInfo = { ...userData };
		if (this.userInfo.idcard) {
			this.isOpen = true;
			this.getuserNumCount();
			this.getpaybankCardList();
		} else {
			this.isOpen = false;
		}
	},
	methods: {
		goToPath(path) {
			uni.navigateTo({
				url: path
			});
		},

		// 获取余额
		async getuserNumCount() {
			try {
				const resData = await userNumCount();
				const orderNum = resData.data;
				if (orderNum.money == '0.00') {
					this.balanceValue = '0.00';
					return false;
				}
				this.balanceValue = formatAmount(Number(orderNum.money));
			} catch (e) {
				throw Error(e);
			}
		},

		// 获取银行卡
		async getpaybankCardList() {
			try {
				const resData = await paybankCardList();
				this.cardList = resData.data;
			} catch (e) {
				throw Error(e);
			}
		},
		
		// 实名认证
		getAuthentication(){
			if(this.isOpen){
				uni.navigateTo({
					url:'/pages/realNameAuthentication/realNameAuthentication'
				})
			}else{
				uni.navigateTo({
					url:'/pages/openWallet/openWallet'
				})
			}
		}
	}
};
</script>

<style>
page {
	height: 100%;
	background-color: #f3f3f3;
}
</style>

<style scoped lang="scss">
.balance-main {
	box-sizing: border-box;
	padding: 20rpx;
	.balance-option {
		display: flex;
		align-items: center;
		height: 140px;
		background-color: #07c160;
		color: #fff;
		border-radius: 20rpx;
		.balance-option-item {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			font-size: 26rpx;
		}
	}
	.accountSetting-Trading {
		background-color: #fff;
		padding: 30rpx;
		padding-bottom: 10rpx;
		border-radius: 24rpx;
		margin-top: 20rpx;
		.settingTitle {
			margin-bottom: 20rpx;
			font-size: 28rpx;
		}

		.settingList {
			.settingItem {
				display: flex;
				align-items: center;
				justify-content: space-between;
				background-color: #fff;
				height: 110rpx;
				border-bottom: 1px #f3f3f3 solid;
				font-size: 30rpx;
				.settingItem-left {
					display: flex;
					align-items: center;
					color: #000;
					.icons {
						margin-right: 4px;
					}
				}
				.settingItem-rigth {
					display: flex;
					align-items: center;
					color: #999;
					font-size: 26rpx;
					.icons {
						margin-left: 2px;
					}
				}
			}
		}
	}
	.balance-btn {
		position: fixed;
		bottom: 0px;
		left: 0px;
		width: 100%;
		box-sizing: border-box;
		padding: 20rpx;
		padding-bottom: 0px;
		background-color: #fff;
	}
}
</style>
